//
//  CustomView.m
//  绘制基本图形
//
//  Created by 伍银荣 on 2021/6/30.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "CustomView.h"

@implementation CustomView



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
  //  [self drawLine];
//    [self drawRectangle];
   // [self drawCirle];
   // [self drawEllipse];
   // [self drawArc];
   // [self drawEvenOdd];
    
   // [self drawNonzeroSurroundNumber];
    
   // [self drawPieChart];
    [self drawBarGraphWithRect:rect];
    
}

//绘制柱状图
- (void)drawBarGraphWithRect:(CGRect)rect {
    NSArray *arr = @[@1, @0.2, @0.5, @0.8, @0.3, @0.7, @0.9];
    CGFloat barWidth = 20;
    for (int i = 0 ; i < arr.count; i++) {
        CGFloat barHeight = [arr[i] floatValue] * rect.size.height;
        CGFloat barX = i * 2 * barWidth;
        CGFloat barY = rect.size.height - barHeight;
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(barX, barY, barWidth, barHeight)];
        [[UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0] set];
        [path fill];
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //点击这个view的时候需要重绘这个界面(相当于tableView的刷新一样)
    [self setNeedsDisplay];
    //重绘的显示区域
    //[self setNeedsDisplayInRect:CGRectMake(0, 0, 300, 300)];
}

//绘制饼状图
- (void)drawPieChart {
    NSArray *arr = @[@0.2, @0.1, @0.1, @0.4, @0.2];
    CGFloat start = 0;
    CGFloat end = 0;
    for (int i = 0; i < arr.count; i++) {
        //本次绘制的结束弧度 = 起始弧度 + 本次的弧度
        end = 2 * M_PI * [arr[i] floatValue] + start;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(170, 170) radius:100 startAngle:start endAngle:end clockwise:1];
        [path addLineToPoint:CGPointMake(170, 170)];
        
        //绘制每一块饼图的随机色
        [[UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0] set];
        [path fill];
        //每次绘制完一个弧以后都要重新设置下一次的起始弧度为当前的结束弧度
        start = end;
    }
    
}




//非零环绕数
- (void)drawNonzeroSurroundNumber {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:1];
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:0];
    [[UIColor redColor] set];
    CGContextAddPath(ctx, path.CGPath);
    CGContextAddPath(ctx, path1.CGPath);
    
    //默认填充模式:nonzero winding number rule(非零环绕数规则):从左跨到右,+1 .从右跨到左,-1. 最后如果为0,那么不填充,否则填充
    CGContextDrawPath(ctx, kCGPathFill);
    
    
}

//奇填偶不填
- (void)drawEvenOdd {
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 100)];
//    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 150) radius:80 startAngle:0 endAngle:2 * M_PI clockwise:1];
//    UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:CGRectMake(250, 30, 20, 200)];
//    CGContextAddPath(ctx, path2.CGPath);
//    CGContextAddPath(ctx, path1.CGPath);
//    CGContextAddPath(ctx, path.CGPath);
//
//    //说明:被覆盖过的奇数词的点填充,被覆盖过偶次数的点不填充
//    CGContextDrawPath(ctx, kCGPathEOFill);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 100)];
    [path addArcWithCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:1];
    //是否使用奇填偶不填的规则
    path.usesEvenOddFillRule = YES;
    [path fill];
    
}

//5.绘制圆弧:arc
//startAngle:开始位置   endAngle:结束位置   clockwise:是否是顺时针
- (void)drawArc {
    //oc
//    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
//    //拼接路径后一定要连接圆心
//    [path addLineToPoint:CGPointMake(150, 150)];
//    //连接后还要关闭路径
//    [path closePath];
//    [path stroke];
    
    //c
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, 150, 150, 100, 0, M_PI_2, NO);
    //连接圆心
    CGContextAddLineToPoint(ctx, 150, 150);
    //关闭路径
    CGContextClosePath(ctx);
    //渲染
    CGContextStrokePath(ctx);
    
}

//4.绘制椭圆
- (void)drawEllipse {
    //oc
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 100)];
    [path stroke];
    
    //c
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 200, 100));
//    CGContextStrokePath(ctx);
}

//3.绘制圆形
- (void)drawCirle {
    //第一种方法:oc
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:50];
//    [path stroke];
    
    //第二种方法:c
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, 150, 150, 100, 0, M_PI * 2, 1);
    CGContextStrokePath(ctx);
    
}


//2.绘制矩形
- (void)drawRectangle {
    //oc
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 100)];
    [path stroke];
    
    //c
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
//    CGContextMoveToPoint(ctx, 50, 50);
//    CGContextAddLineToPoint(ctx, 200, 50);
//    CGContextAddLineToPoint(ctx, 200, 100);
//    CGContextAddLineToPoint(ctx, 50, 100);
//    CGContextAddLineToPoint(ctx, 50, 50);
//    CGContextStrokePath(ctx);
    
}

//1.绘制直线
- (void)drawLine {
    //c语言
    //获取图形上下文对象
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //拼接路径,将路径添加到图形上下文对象中
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx, 150, 50);
    //设置线宽
    CGContextSetLineWidth(ctx, 10);
    //设置连接处状态
//    kCGLineJoinMiter,  默认
//    kCGLineJoinRound,  圆角
//    kCGLineJoinBevel   切角
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    //设置头尾处的样式
//    kCGLineCapButt,   默认
//    kCGLineCapRound,  圆角
//    kCGLineCapSquare  会比默认的样式两遍各多一个线宽/2的距离
    CGContextSetLineCap(ctx, kCGLineCapSquare);
   
    
    
   
    
    //设置填充颜色
    [[UIColor redColor] setFill];
    //设置描边颜色
    [[UIColor blueColor] setStroke];
    
    //渲染(将图形上下文的图形绘制到对应的设备上)
    
    //设置填充渲染
    //    CGContextFillPath(ctx);
    //    //设置描边渲染
    //    CGContextStrokePath(ctx);
    
    //填充
   // CGContextDrawPath(ctx, kCGPathFill);
    
    //描边
  //  CGContextDrawPath(ctx, kCGPathStroke);
    
    
    //设置既描边有填充
    
//    kCGPathFill,   填充
//    kCGPathEOFill,
//    kCGPathStroke,  描边
//    kCGPathFillStroke,   既填充又描边
//    kCGPathEOFillStroke
    CGContextDrawPath(ctx, kCGPathFillStroke);
    
    
    //oc
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(50, 50)];
//    [path addLineToPoint:CGPointMake(100, 100)];
//    [path addLineToPoint:CGPointMake(150, 50)];
//    //设置线宽
//    [path setLineWidth:10];
//    //设置头尾处的演示
//    [path setLineCapStyle:kCGLineCapRound];
//    //设置连接处的样式
//    [path setLineJoinStyle:kCGLineJoinRound];
//
//    //设置填充的颜色
////    [[UIColor redColor] setFill];
////    //设置描边的颜色
////    [[UIColor orangeColor] setStroke];
//
//    //同时设置描边和填充的颜色
//    [[UIColor redColor] set];
//
//    //渲染的两种方式:两种都写上就是既填充又描边
//    //1.描边
//    [path stroke];
//    //2.填充
//    [path fill];
}


@end
